using UnityEngine;
using QFramework;

namespace Projectsurviva
{
    public partial class Play : ViewController
    {
        private float movespd = 2.5f;
        public static Play Default;
        private void Awake()
        {
            Default = this;
        }
        private void OnDestroy()
        {
            Default = null;
        }
        void Start()
        {
            // Code Here
            "hello QFramework".LogInfo();
            Hurtbox.OnTriggerEnter2DEvent(
                (collider) =>
                {
                    var hitbox = collider.GetComponent<HitBox>();
                    if (hitbox)
                    {
                        if (hitbox.owner.gameObject.CompareTag("Enemy"))
                        {
                            Global.Hp.Value--;
                            if (Global.Hp.Value <= 0)
                            {

                                this.DestroyGameObjGracefully();
                                // ResKit.Init();
                                UIKit.OpenPanel<UiGameOverpanel>();
                                AudioKit.PlaySound("die");
                            }
                            else
                            {
                                AudioKit.PlaySound("hurt");
                            }
                        }
                    }

                }

                ).UnRegisterWhenGameObjectDestroyed(gameObject);

            void UpdataHp()
            {
                HpValue.fillAmount = Global.Hp.Value / (float)Global.maxHp.Value;
            }
            Global.Hp.RegisterWithInitValue(hp =>
            {
                UpdataHp();
            }).UnRegisterWhenGameObjectDestroyed(gameObject);
            Global.maxHp.RegisterWithInitValue(maxhp =>
            {
                UpdataHp();

            }).UnRegisterWhenGameObjectDestroyed(gameObject);
        }
        private void Update()
        {

            float h = Input.GetAxis("Horizontal");
            float v = Input.GetAxis("Vertical");
            var direction = new Vector2(h, v).normalized;
            SelfRigidbody2D.velocity = direction * movespd*Global.MoveMentSpeedRate.Value ;
          
        }
    }
}
